//---------------------------------------------------------------------------
// Sword3 Engine (c) 1999-2000 by Kingsoft
//
// File:	KBitmap16.cpp
// Date:	2000.08.08
// Code:	Daniel Wang
// Desc:	256 Color bitmap file class
//---------------------------------------------------------------------------
#include "stdafx.h"
#include "KBitmap16.h"

BOOL KBitmap16::init(int nWidth, int nHeight)
{
	m_nWidth  = nWidth;
	m_nHeight = nHeight;
	m_Bitmap.alloc(m_nWidth * m_nHeight * 2);
	return (NULL != m_Bitmap.GetBuffer());
}

void KBitmap16::free()
{
	m_Bitmap.free();
}

void KBitmap16::clear()
{
	m_Bitmap.zero();
}

void KBitmap16::draw(int x, int y)
{
	LPVOID lpBitmap = m_Bitmap.GetBuffer();
	if (lpBitmap)
	{
		//g_DrawBitmap16(x, y, m_nWidth, m_nHeight, lpBitmap);
	}
}

void KBitmap16::DrawLum(int x, int y, int nLum)
{
	LPVOID lpBitmap = m_Bitmap.GetBuffer();
	if (lpBitmap)
	{
		//g_DrawBitmap16Lum(x, y, m_nWidth, m_nHeight, nLum, lpBitmap);
	}
}

void KBitmap16::PutPixel(int x, int y, WORD color)
{
	assert((x >= 0 && x < m_nWidth && y >= 0 && y < m_nHeight));

	if (x >= 0 && x < m_nWidth && y >= 0 && y < m_nHeight)
		((LPWORD)m_Bitmap.GetBuffer())[y * m_nWidth + x] = color;
}

WORD KBitmap16::GetPixel(int x, int y)
{
	assert((x >= 0 && x < m_nWidth && y >= 0 && y < m_nHeight));

	/*if (x >= 0 && x < m_nWidth && y >= 0 && y < m_nHeight)
		return ((LPWORD)m_Bitmap.GetBuffer())[y * m_nWidth + x];
	else
		return 0;
	*/

	return ((LPWORD)m_Bitmap.GetBuffer())[y * m_nWidth + x];
}

LPWORD KBitmap16::GetPointer(int x, int y)
{
	assert((x >= 0 && x < m_nWidth && y >= 0 && y < m_nHeight));

	/*if (x >= 0 && x < m_nWidth && y >= 0 && y < m_nHeight)
		return (LPWORD)m_Bitmap.GetBuffer() + (y * m_nWidth + x);
	else
		return NULL;
	*/

	return (LPWORD)m_Bitmap.GetBuffer() + (y * m_nWidth + x);
}

BOOL KBitmap16::load(krt::stream &src)
{
	KBMPHEADER16	header;

	src.read(&header, sizeof(header));
	
	if (header.signature != BM16_SIGNATURE)
		return FALSE;

	if (!init(header.width, header.height))
		return FALSE;

	src.read(m_Bitmap.GetBuffer(), m_nWidth * m_nHeight * 2);
	
	/*if (g_pDirectDraw->GetRGBBitMask16() != (DWORD)Header.RGBMask)
	{
		if (RGB_565 == Header.RGBMask)
			g_565To555(m_nWidth, m_nHeight, m_Bitmap.GetBuffer());
		else
			g_555To565(m_nWidth, m_nHeight, m_Bitmap.GetBuffer());
	}
	*/

	return TRUE;
}

BOOL KBitmap16::load(LPCSTR FileName)
{
	/*KPakFile		File;
	KBMPHEADER16	Header;

	File.Open(FileName);
	File.Read(&Header, sizeof(Header));
	
	if (!g_MemComp(Header.Id, "BM16", 4))
		return FALSE;

	if (!Init(Header.Width, Header.Height))
		return FALSE;

	File.Read(m_Bitmap.GetBuffer(), m_nWidth * m_nHeight * 2);
	
	if (g_pDirectDraw->GetRGBBitMask16() != (DWORD)Header.RGBMask)
	{
		if (RGB_565 == Header.RGBMask)
			g_565To555(m_nWidth, m_nHeight, m_Bitmap.GetBuffer());
		else
			g_555To565(m_nWidth, m_nHeight, m_Bitmap.GetBuffer());
	}*/
	return TRUE;
}

BOOL KBitmap16::save(LPCSTR FileName)
{
	KBMPHEADER16 header;
	krt::FileStream	fs;
	
	if (!fs.open(FileName, krt::FileStream::fmCreate))
		return FALSE;

	header.signature = BM16_SIGNATURE;
	header.width = m_nWidth;
	header.height = m_nHeight;
	//header.RGBMask = g_pDirectDraw->GetRGBBitMask16();

	fs.write(&header, sizeof(header));
	fs.write(m_Bitmap.GetBuffer(), m_nWidth * m_nHeight * 2);

	return TRUE;
}
